package com.itlong.cloud.rabbitmq.consumer;

import com.alibaba.fastjson.JSON;
import com.itlong.cloud.POJO.PO.PropertyRoleProjectPO;
import com.itlong.cloud.POJO.PO.TimeTaskDeadMessageQueuePO;
import com.itlong.cloud.constants.RabbitMQConstant;
import com.itlong.cloud.sdk.service.ISDKServerOldUserService;
import com.itlong.cloud.utils.node.JsonUtil;
import com.rabbitmq.client.Channel;
import org.slf4j.Logger;import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;


/**
 * <desc>
 *      SDK申请项目时，保存独立开发者与项目之间的关系队列消费者
 * </desc>
 *
 * @createDate 2017/10/17
 */
@Component
public class SDKSaveDeveloperConsumer {
    private static final Logger LOG = LoggerFactory.getLogger(SDKSaveDeveloperConsumer.class);

    @Autowired
    ISDKServerOldUserService iSDKUserService;

    /**
     * <desc>
     *      SDK申请项目时，保存独立开发者与项目之间的关系队列消费者
     * </desc>
     * @param message 消息
     * @param channel
     * @author Biao.R
     * @createDate 2017/11/11
     */
    @RabbitListener(queues = RabbitMQConstant.RAABITMQ_SDK_SAVEDEVELOPER_QUENUE)
    public void process(Message message, Channel channel) throws InterruptedException, IOException {
        byte[] body = message.getBody();
        //LOG.info("RabbitMQ_SDK申请项目时，保存独立开发者与项目之间的关系队列消费者:" + body);
        try {
            TimeTaskDeadMessageQueuePO timeTaskDeadMessageQueuePO = JsonUtil.toObject(new String(body),
                    TimeTaskDeadMessageQueuePO.class);
            PropertyRoleProjectPO roleProjectPO = JSON.parseObject(JSON.toJSONString(JSON.parseObject(timeTaskDeadMessageQueuePO.getMessageContent()).get("data")), PropertyRoleProjectPO.class);
            if (roleProjectPO != null) {
                Integer saveCount = iSDKUserService.saveRoleProject(roleProjectPO);
            }
        } catch (Exception e) {
            channel.basicReject(message.getMessageProperties().getDeliveryTag(), false);
            LOG.error("【RabbitMQ_SDK申请项目时，保存独立开发者与项目之间的关系队列消费者】:处理异常", e);
        } finally {
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        }
    }
}
